package com.gao.weblog.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gao.weblog.admin.model.vo.tag.AddTagReqVO;
import com.gao.weblog.admin.model.vo.tag.DeleteTagReqVO;
import com.gao.weblog.admin.model.vo.tag.FindTagPageListReqVO;
import com.gao.weblog.admin.model.vo.tag.FindTagPageListRspVO;
import com.gao.weblog.admin.model.vo.tag.SearchTagsReqVO;
import com.gao.weblog.admin.service.AdminTagService;
import com.gao.weblog.common.domain.dos.TagDO;
import com.gao.weblog.common.domain.mapper.ArticleTagRelMapper;
import com.gao.weblog.common.domain.mapper.TagMapper;
import com.gao.weblog.common.enums.ResponseCodeEnum;
import com.gao.weblog.common.model.vo.SelectRspVO;
import com.gao.weblog.common.utils.PageResponse;
import com.gao.weblog.common.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AdminTagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements AdminTagService {

    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private ArticleTagRelMapper articleTagRelMapper;

    @Override
    public Response addTags(AddTagReqVO addTagReqVO) {

        List<TagDO> tagDOS = addTagReqVO.getTags()
                .stream()
                .map(tagName -> TagDO.builder()
                        .name(tagName.trim())
                        .createTime(LocalDateTime.now())
                        .updateTime(LocalDateTime.now())
                        .build())
                .collect(Collectors.toList());
        try {
            saveBatch(tagDOS);
        }catch (Exception e){
            log.warn("该标签已存在", e);
        }

        return Response.success();
    }

    @Override
    public PageResponse findTagPageList(FindTagPageListReqVO findTagPageListReqVO) {

        Long current = findTagPageListReqVO.getCurrent();
        Long size = findTagPageListReqVO.getSize();
        String tagName = findTagPageListReqVO.getName();
        LocalDate tagStartDate = findTagPageListReqVO.getStartDate();
        LocalDate tagEndDate = findTagPageListReqVO.getEndDate();

        Page<TagDO> page = tagMapper.selectPageList(current, size, tagName, tagStartDate, tagEndDate);

        List<TagDO> tagDOS = page.getRecords();

        List<FindTagPageListRspVO> vos = null;

        if (!CollectionUtils.isEmpty(tagDOS)) {
            vos = tagDOS.stream()
                    .map(tagDO -> FindTagPageListRspVO.builder()
                    .id(tagDO.getId())
                    .name(tagDO.getName())
                    .createTime(tagDO.getCreateTime())
                    .build())
                    .collect(Collectors.toList());
        }

        return PageResponse.success(page, vos);
    }

    @Override
    public Response deleteTag(DeleteTagReqVO deleteTagReqVO) {

        int count = tagMapper.deleteById(deleteTagReqVO.getId());

        return count == 1 ? Response.success() : Response.fail(ResponseCodeEnum.TAG_NOT_EXISTED);
    }

    @Override
    public Response searchTags(SearchTagsReqVO searchTagsReqVO) {
        String key =  searchTagsReqVO.getKey();
        List<TagDO> tagDOS = tagMapper.selectByKey(key);
        List<SelectRspVO> vos = null;
        if (CollectionUtils.isNotEmpty(tagDOS)) {
            vos = tagDOS.stream().map(tagDO ->
                    SelectRspVO.builder()
                            .label(tagDO.getName())
                            .value(tagDO.getId())
                            .build())
                    .collect(Collectors.toList());
        }
        return Response.success(vos);
    }

    @Override
    public Response findTagSelectList() {
        List<TagDO> tagDOS = tagMapper.selectList(null);
        List<SelectRspVO> selectRspVOS = null;
        if (!CollectionUtils.isEmpty(tagDOS)) {
            selectRspVOS = tagDOS.stream()
                    .map(tagDO -> SelectRspVO.builder()
                            .label(tagDO.getName())
                            .value(tagDO.getId())
                            .build())
                    .collect(Collectors.toList());
        }
        return Response.success(selectRspVOS);
    }
}
